package com.jianyun.jyzs.http;

import com.google.gson.Gson;
import com.jianyun.jyzs.bean.EngineeringResult;
import com.jianyun.jyzs.dao.EngineerDao;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by mlli on 2017/11/11.
 * 个人动态的帮助类 从服务器下载个人数据到本地
 */

public class PDHttpHelper {
    public interface HttpResultListener{
        void resultListener(boolean b, String msg);
    }

    /**
     * 获取个人动态
     * @param dao
     * @param map
     * @param listener
     */
    public static void getPersonalDynamic(final EngineerDao dao, HashMap<String, String> map, final HttpResultListener listener) {
        Api api = RetrofitHelper.builderCeshiHttp().create(Api.class);
        api.getNewPersonDynamic(map).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<EngineeringResult>() {
                    @Override
                    protected void onResponse(EngineeringResult response) {
                        if (response.isResult()) {
                            Gson gson = new Gson();
                            List<EngineeringResult.EngineerBean> list = response.getList();

                            for (EngineeringResult.EngineerBean ee : list
                                    ) {
                                ee.setListAgress(gson.toJson(ee.getAgress()));
                                ee.setListAttachment(gson.toJson(ee.getAttachment()));
                                ee.setListDisscuss(gson.toJson(ee.getDiscuss()));

                                List<EngineeringResult.EngineerBean.AttachmentBean> attachment = ee.getAttachment();
                                ArrayList<String> images = new ArrayList<>();
                                for (EngineeringResult.EngineerBean.AttachmentBean eea : attachment
                                        ) {
                                    if (eea.getPhotoPath() != null) images.add(eea.getId());
                                }
                                ee.setListImages(gson.toJson(images));
                                ee.setDeleted(response.isDeleted());
                                dao.insert(ee);
                            }
                            listener.resultListener(true,null);
                        } else {
                            listener.resultListener(true,response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        RceLog.d("lml", msg);
                        listener.resultListener(true,msg);
                    }
                });

    }
}
